{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d852ca9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 特征缩放\n",
    "import numpy as np\n",
    "from sklearn import preprocessing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1217ea46",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.        ]\n",
      " [0.28571429]\n",
      " [0.35714286]\n",
      " [0.42857143]\n",
      " [1.        ]]\n"
     ]
    }
   ],
   "source": [
    "# 创建特征\n",
    "feature = np.array([[-500.5],\n",
    "                    [-100.1],\n",
    "                    [0],\n",
    "                    [100.1],\n",
    "                    [900.9]])\n",
    "# 创建缩放器\n",
    "minmax_scale = preprocessing.MinMaxScaler(feature_range=(0, 1))\n",
    "# 缩放特征的值\n",
    "scaled_feature = minmax_scale.fit_transform(feature)\n",
    "print(scaled_feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "76f3e42e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.76058269]\n",
      " [-0.54177196]\n",
      " [-0.35009716]\n",
      " [-0.32271504]\n",
      " [ 1.97516685]]\n"
     ]
    }
   ],
   "source": [
    "## 特征标准化\n",
    "# 平均值0 标准差1\n",
    "x  = np.array([[-1000.1],\n",
    "               [-200.2],\n",
    "               [500.5],\n",
    "               [600.6],\n",
    "               [9000.9]])\n",
    "# 创建缩放器\n",
    "scaler = preprocessing.StandardScaler()\n",
    "standardlized = scaler.fit_transform(x)\n",
    "print(standardlized)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b0b8903d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean:  0\n",
      "Standard deviation:  1.0\n"
     ]
    }
   ],
   "source": [
    "## 打印平均值和标准差\n",
    "print(\"Mean: \", round(standardlized.mean()))\n",
    "print(\"Standard deviation: \", standardlized.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d0ccd127",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.87387612],\n",
       "       [-0.875     ],\n",
       "       [ 0.        ],\n",
       "       [ 0.125     ],\n",
       "       [10.61488511]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 中位数和四分位数间距进行缩放\n",
    "robust_scaler = preprocessing.RobustScaler()\n",
    "robust_scaler.fit_transform(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ed2eb71e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.70710678, 0.70710678],\n",
       "       [0.30782029, 0.95144452],\n",
       "       [0.07405353, 0.99725427],\n",
       "       [0.04733062, 0.99887928],\n",
       "       [0.95709822, 0.28976368]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 归一化观察值\n",
    "from sklearn.preprocessing import Normalizer\n",
    "# 创建特征矩阵\n",
    "features = np.array([[0.5, 0.5],\n",
    "                     [1.1, 3.4],\n",
    "                     [1.5, 20.2],\n",
    "                     [1.63, 34.4],\n",
    "                     [10.9, 3.3]])\n",
    "# 创建归一化器\n",
    "normalizer = Normalizer(norm=\"l2\")\n",
    "# 转换特征矩阵\n",
    "normalizer.fit_transform(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f3c7afff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.5       , 0.5       ],\n",
       "       [0.24444444, 0.75555556],\n",
       "       [0.06912442, 0.93087558],\n",
       "       [0.04524008, 0.95475992],\n",
       "       [0.76760563, 0.23239437]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 曼哈顿范数\n",
    "feature_l1_norm = Normalizer(norm=\"l1\").transform(features)\n",
    "feature_l1_norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2b5d5e87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sum of the first observation's vlues:  1.0\n"
     ]
    }
   ],
   "source": [
    "print(\"Sum of the first observation's vlues: \", feature_l1_norm[0, 0] + feature_l1_norm[0, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7de51f47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2., 3., 4., 6., 9.],\n",
       "       [2., 3., 4., 6., 9.],\n",
       "       [2., 3., 4., 6., 9.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 生成多项式和交互特征\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "features = np.array([[2, 3],\n",
    "                     [2, 3],\n",
    "                     [2, 3]])\n",
    "# 创建PolynomialFeatures对象\n",
    "polynomial_interaction = PolynomialFeatures(degree=2, include_bias=False)\n",
    "# degree=2  ==>  x1 x2 x1^2 x1*x2 x2^2\n",
    "# 创建特征多项式\n",
    "polynomial_interaction.fit_transform(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "87aa2d46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2., 3., 6.],\n",
       "       [2., 3., 6.],\n",
       "       [2., 3., 6.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# interaction_only=True 强制创建出来的特征值包含交互特征\n",
    "PolynomialFeatures(degree=2, interaction_only=True, include_bias=False).fit_transform(features)\n",
    "# x1 x2 x1*x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3ded0851",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12, 13],\n",
       "       [12, 13],\n",
       "       [12, 13]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 转换特征\n",
    "from sklearn.preprocessing import FunctionTransformer\n",
    "features = np.array([[2, 3],\n",
    "                     [2, 3],\n",
    "                     [2, 3]])\n",
    "def add_ten(x):\n",
    "    return x + 10\n",
    "# 创建特征转换器\n",
    "ten_transformer = FunctionTransformer(add_ten)\n",
    "# 转换特征矩阵\n",
    "ten_transformer.fit_transform(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "23395d71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   feature_1  feature_2\n",
      "0          2          3\n",
      "1          2          3\n",
      "2          2          3\n",
      "   feature_1  feature_2\n",
      "0         12         13\n",
      "1         12         13\n",
      "2         12         13\n"
     ]
    }
   ],
   "source": [
    "## pandas中用的是apply\n",
    "import pandas as pd\n",
    "df = pd.DataFrame(\n",
    "    features,\n",
    "    columns=[\"feature_1\", \"feature_2\"]\n",
    ")\n",
    "print(df)\n",
    "df = df.apply(add_ten)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a4e1fd95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1,  1,  1,  1,  1,  1,  1,  1,  1,  1])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 识别异常值\n",
    "from sklearn.covariance import EllipticEnvelope\n",
    "from sklearn.datasets import make_blobs\n",
    "# 创建模拟数据\n",
    "features, _ = make_blobs(n_samples=10,\n",
    "                         n_features=2,\n",
    "                         centers=1,\n",
    "                         random_state=1)\n",
    "# 将第一个观察值的值替换为极端值\n",
    "features[0, 0] = 10000\n",
    "features[0, 1] = 10000\n",
    "# 创建识别器\n",
    "outlier_detector = EllipticEnvelope(contamination=.1)  # 参数是污染指数\n",
    "# 拟合识别器\n",
    "outlier_detector.fit(features)\n",
    "# 预测异常值\n",
    "outlier_detector.predict(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3742212b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0], dtype=int64),)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个特征\n",
    "feature = features[:, 0]\n",
    "# 创建一个函数来返回异常值的下标\n",
    "def indicies_of_outliers(x):\n",
    "    # 第一个四分位 和 第三个四分位\n",
    "    q1, q3 = np.percentile(x, [25, 75])\n",
    "    # 之差是IRR\n",
    "    iqr = q3 - q1\n",
    "    lower_round = q1 - (iqr * 1.5)\n",
    "    upper_round = q3 + (iqr * 1.5)\n",
    "    return np.where((x > upper_round) | (x <lower_round))\n",
    "indicies_of_outliers(feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d7a80531",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "      <th>Bathrooms</th>\n",
       "      <th>Square_Feet</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>534433</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>392333</td>\n",
       "      <td>3.5</td>\n",
       "      <td>2500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>293222</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Price  Bathrooms  Square_Feet\n",
       "0  534433        2.0         1500\n",
       "1  392333        3.5         2500\n",
       "2  293222        2.0         1500"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 处理异常值\n",
    "import pandas as pd\n",
    "houses = pd.DataFrame()\n",
    "houses[\"Price\"] = [534433, 392333, 293222, 4322032]\n",
    "houses[\"Bathrooms\"] = [2, 3.5, 2, 116]\n",
    "houses[\"Square_Feet\"] = [1500, 2500, 1500, 48000]\n",
    "\n",
    "# 筛选观察值\n",
    "houses[houses[\"Bathrooms\"] < 20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "aa39b119",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "      <th>Bathrooms</th>\n",
       "      <th>Square_Feet</th>\n",
       "      <th>Outlier</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>534433</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1500</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>392333</td>\n",
       "      <td>3.5</td>\n",
       "      <td>2500</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>293222</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1500</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4322032</td>\n",
       "      <td>116.0</td>\n",
       "      <td>48000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Price  Bathrooms  Square_Feet  Outlier\n",
       "0   534433        2.0         1500        0\n",
       "1   392333        3.5         2500        0\n",
       "2   293222        2.0         1500        0\n",
       "3  4322032      116.0        48000        1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标记异常值\n",
    "houses[\"Outlier\"] = np.where(houses[\"Bathrooms\"] < 20, 0, 1)\n",
    "houses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9c9a97c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "      <th>Bathrooms</th>\n",
       "      <th>Square_Feet</th>\n",
       "      <th>Outlier</th>\n",
       "      <th>Log_Of_Square_Feet</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>534433</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1500</td>\n",
       "      <td>0</td>\n",
       "      <td>7.313220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>392333</td>\n",
       "      <td>3.5</td>\n",
       "      <td>2500</td>\n",
       "      <td>0</td>\n",
       "      <td>7.824046</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>293222</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1500</td>\n",
       "      <td>0</td>\n",
       "      <td>7.313220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4322032</td>\n",
       "      <td>116.0</td>\n",
       "      <td>48000</td>\n",
       "      <td>1</td>\n",
       "      <td>10.778956</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Price  Bathrooms  Square_Feet  Outlier  Log_Of_Square_Feet\n",
       "0   534433        2.0         1500        0            7.313220\n",
       "1   392333        3.5         2500        0            7.824046\n",
       "2   293222        2.0         1500        0            7.313220\n",
       "3  4322032      116.0        48000        1           10.778956"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对异常值进行特征转换 降低异常值的影响\n",
    "houses[\"Log_Of_Square_Feet\"] = [np.log(x) for x in houses[\"Square_Feet\"]]\n",
    "houses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7115669a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0],\n",
       "       [0],\n",
       "       [1],\n",
       "       [1],\n",
       "       [1]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 将特征离散化\n",
    "# 根据阈值将特征二值化\n",
    "from sklearn.preprocessing import Binarizer\n",
    "age = np.array([[6], [12], [20], [36], [65]])\n",
    "# 创建二值化器\n",
    "binarizer = Binarizer(threshold=18)\n",
    "# 抓换特征\n",
    "binarizer.fit_transform(age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f6ab1448",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>feature_1</th>\n",
       "      <th>feature_2</th>\n",
       "      <th>group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-9.877554</td>\n",
       "      <td>-3.336145</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-7.287210</td>\n",
       "      <td>-8.353986</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-6.943061</td>\n",
       "      <td>-7.023744</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-7.440167</td>\n",
       "      <td>-8.791959</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-6.641388</td>\n",
       "      <td>-8.075888</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   feature_1  feature_2  group\n",
       "0  -9.877554  -3.336145      0\n",
       "1  -7.287210  -8.353986      2\n",
       "2  -6.943061  -7.023744      2\n",
       "3  -7.440167  -8.791959      2\n",
       "4  -6.641388  -8.075888      2"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 使用聚类的方式将观察值分组\n",
    "import pandas as pd\n",
    "from sklearn.datasets import make_blobs\n",
    "from sklearn.cluster import KMeans\n",
    "\n",
    "# 创建模拟的特征矩阵\n",
    "features, _ = make_blobs(n_samples=50,\n",
    "                         n_features=2,\n",
    "                         centers=3,\n",
    "                         random_state=1)\n",
    "# 创建数据帧\n",
    "df = pd.DataFrame(features, columns=[\"feature_1\", \"feature_2\"])\n",
    "# 创建K-Means聚类其\n",
    "cluster = KMeans(3, random_state=0)\n",
    "# 将聚类器应用在特征上\n",
    "cluster.fit(features)\n",
    "# 预测聚类的值\n",
    "df[\"group\"] = cluster.predict(features)\n",
    "# 查看前几个观察值\n",
    "df.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3c47148b",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 删除带有缺失值的观察值\n",
    "import numpy as np\n",
    "features = np.array([[1.1, 11.1],\n",
    "                     [2.2, 22.2],\n",
    "                     [3.3, 33.3],\n",
    "                     [4.4, 44.4],\n",
    "                     [np.nan, 55.5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "82235695",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.1, 11.1],\n",
       "       [ 2.2, 22.2],\n",
       "       [ 3.3, 33.3],\n",
       "       [ 4.4, 44.4]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 只保留没有缺失值的观察值\n",
    "features[~np.isnan(features).any(axis=1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "324c4c58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>feature_1</th>\n",
       "      <th>feature_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.1</td>\n",
       "      <td>11.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.2</td>\n",
       "      <td>22.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3.3</td>\n",
       "      <td>33.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.4</td>\n",
       "      <td>44.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   feature_1  feature_2\n",
       "0        1.1       11.1\n",
       "1        2.2       22.2\n",
       "2        3.3       33.3\n",
       "3        4.4       44.4"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用pandas丢弃有缺失值的观察值\n",
    "import pandas as pd\n",
    "df = pd.DataFrame(features, columns=[\"feature_1\", \"feature_2\"])\n",
    "df.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "43034aa8",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 填充缺失值\n",
    "import numpy as np\n",
    "from fancyimpute import KNN\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.datasets import make_blobs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "50b61a53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True Value:  0.8730186113995938\n",
      "Imputed Value:  1.0955332713113226\n"
     ]
    }
   ],
   "source": [
    "# 创建模拟特征矩阵\n",
    "features, _ = make_blobs(n_samples=1000, n_features=2, random_state=1)\n",
    "# 标准化特征\n",
    "scaler = StandardScaler()\n",
    "standardized_features = scaler.fit_transform(features)\n",
    "# 将第一个特征向量的第一个值替换为缺失值\n",
    "true_value = standardized_features[0, 0]\n",
    "standardized_features[0, 0] = np.nan\n",
    "# 预测特征矩阵中的缺失值\n",
    "features_knn_imputed = KNN(k=5, verbose=0).fit_transform(standardized_features)\n",
    "# 对比真实值和填充值\n",
    "print(\"True Value: \", true_value)\n",
    "print(\"Imputed Value: \", features_knn_imputed[0, 0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1fd62f26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True Value:  0.8730186113995938\n",
      "Imputed Value:  -3.058372724614996\n"
     ]
    }
   ],
   "source": [
    "## 用特征的平均数 中位数或者众数来填充缺失值\n",
    "from sklearn.impute import SimpleImputer\n",
    "# 创建填充器\n",
    "mean_imputer = SimpleImputer(strategy=\"mean\")\n",
    "features_mean_imputed = mean_imputer.fit_transform(features)\n",
    "print(\"True Value: \", true_value)\n",
    "print(\"Imputed Value: \", features_mean_imputed[0, 0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
